package file;

import org.junit.Test;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @author lwmzzz
 * @version 1.0
 * @date 2021/4/5 17:44
 * @description file类test
 */
public class FileTest {
    @Test
    public void test1() {
        //absolute path or relative path
        File file = new File("");
        //get path from parent,then concat with child path
        File file1 = new File(file, "");
        //ParentPath+ChildPath
        File file2 = new File("", "");
    }

    @Test
    public void test2() {
        /*about separator*/
        //UNIX and URI '/'
        //Windows '\'
        File file = new File("C:" + File.separator + "1.txt");
        System.out.println(file.getAbsoluteFile());
    }

    @Test
    public void test3() {
        File file = new File("src\\file\\resource\\1.txt");
        System.out.println(file.getAbsoluteFile());
        System.out.println(file.getName());
        System.out.println(file.getPath());
        System.out.println(file.getParent());
        System.out.println(LocalDateTime.ofInstant(Instant.ofEpochSecond(file.lastModified()), ZoneId.systemDefault()));
        File parentFile = file.getParentFile();
        System.out.println(file.mkdirs());
        System.out.println(file.length());
        System.out.println(file.exists());
        System.out.println(file.canRead());
        for (String s : file.getParentFile().list()) {
            System.out.println(s);
        }
        System.out.println(new File("src\\file\\resource\\folder").mkdirs());
    }

    @Test
    public void test4() {
        System.out.println(new File("src\\file\\resource\\folder").delete());
        try {
            System.out.println(new File("src\\file\\resource\\testfile").createNewFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void exercise1() {
        /*print filename end with '.jpg'*/
        for (String s : new File("src\\file\\exercise\\exercise1").list()) {
            if (s.endsWith(".jpg")) {
                // not regular
                System.out.println(s);
            }
        }
    }

    @Test
    public void exercise2() throws IOException {
        /*nesting delete*/
        File file = new File("src\\file\\exercise\\exercise2\\1\\3\\4\\5\\6\\7");
        file.mkdirs();
        new File(file, "\\1.txt").createNewFile();
        new File("src\\file\\exercise\\exercise2\\1\\3\\4\\5\\6", "123.txt").createNewFile();
        File file1 = new File("src\\file\\exercise\\exercise2");
        deleteFiles(file1);
    }

    @Test
    public void exercise3(){
        /*print filename end with '.jpg'*/
        for (File file : new File("src\\file\\exercise\\exercise1").listFiles((dir, name) -> name.endsWith(".jpg"))) {
            System.out.println(file.getName());
        }
    }

    private void deleteFiles(File file1) {
        if (file1.isDirectory()) {
            for (File file : file1.listFiles()) {
                deleteFiles(file);
            }
        }
        file1.delete();
    }


}
